package com.amz.analyzer.util;

import com.amz.analyzer.constants.StatusCode;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * <p>
 * 描述: 操作结果工具类
 * </p>
 *
 * @author Freddy
 * @version 1.0
 * @since 2020/7/12 14:35
 */
public class Result<T> {

    private boolean flag;//是否成功
    private Integer code;//返回码
    private String message;//返回消息

    private T data;//返回数据

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功";
    }

    public static <T> Result<T> success() {
        return success((T) null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T, R> Result success(IPage<T> data) {
        Result<IPage<T>> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data, String desc) {
        Result<T> success = Result.success(data);
        success.setMessage(desc);
        return success;
    }

    public static <T> Result success(List<T> data) {
        Result<List<T>> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<List<T>> success(List<T> data, String desc) {
        Result<List<T>> success = Result.success(data);
        success.setMessage(desc);
        return success;
    }

    public static <T> Result<T> failure() {
        return failure((T) null);
    }

    public static <T> Result<T> failure(T data, String desc) {
        Result<T> failure = Result.failure(data);
        failure.setMessage(desc);
        return failure;
    }

    public static <T> Result<T> failure(T data) {
        Result<T> result = new Result<>(false, StatusCode.ERROR, "操作失败", data);
        result.setData(data);
        return result;
    }

    public static <T> Result<List<T>> failure(List<T> data, String desc) {
        Result<List<T>> failure = Result.failure(data);
        failure.setMessage(desc);
        return failure;
    }

    public static <T> Result<List<T>> failure(List<T> data) {
        Result<List<T>> result = new Result<>(false, StatusCode.ERROR, "操作失败", data);
        result.setData(data);
        return result;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
